/*************************************************************************
 *                                                                       *
 * This file is part of the FreeMAPs distribution.  For full copyright   *
 * information, see COPYRIGHT and COPYING.LESSER.                        *
 *                                                                       *
 * Author:           sqbang                                              *
 * Email:            sqbang@ustc.edu                                     *
 * Date:             2011.03.30                                          *
 *                                                                       *
 *************************************************************************/

#ifndef FreeMAPs_include_COMMON_tbox_StringToEnum_h
#define FreeMAPs_include_COMMON_tbox_StringToEnum_h

#include "FreeMAPsConfig.h"

//C++ headers
#include <string>

//COMMON headers
#include "COMMON/tbox/Bimap.h"

namespace FreeMAPs
{

namespace Utilities
{

template <typename T>
extern void
init_string_to_enum (
    Bimap<std::string, T>& se);

/**
 * Takes the string \p s and returns the matching
 * enumeration of type \p T.
 */
template <typename T>
T
string_to_enum (
    const std::string& s)
{
    Bimap<std::string, T> string_to_enum;
    init_string_to_enum (string_to_enum);

    if (string_to_enum.has_key (s))
    {
        return string_to_enum.get_value (s);
    }
    else
    {
        return string_to_enum.get_value ("INVALID_VALUE");
    }
}


template <typename T>
std::string
enum_to_string (
    const T e)
{
    Bimap<std::string, T> string_to_enum;
    init_string_to_enum (string_to_enum);

    return string_to_enum.get_key (e);
}

}

}

#include "COMMON/tbox/StringToEnum.inl"

#endif
